# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import os

import pygame as p
import sys
from pygame import RESIZABLE
width,height=702,870
x=50
y=50
os.environ['SDL_VIDEO_WINDOW_POS']='%d,%d'%(x,y)
p.init()
p.time.delay(100)
font=p.font.SysFont('方正粗黑宋简体', 15)
store_font=p.font.SysFont('方正粗黑宋简体',50)
pos = [[150, 118], [91, 168], [115, 238], [196, 249], [217, 346], [303, 395], [331, 430], [343, 500], [420, 525],
       [354, 635], [350, 727], [488, 766], [406, 718], [466, 720], [459, 635], [541, 631], [516, 511], [574, 511],
       [628, 470], [608, 445], [527, 353], [410, 419], [406, 318], [331, 286], [242, 152], [411, 155], [507, 163]]
weather = ['1', '1', '0', '2', '0', '1', '2', '0', '1', '1', '2', '1', '0', '1', '1', '1', '2', '2', '1', '1', '0', '0',
           '1', '0', '2', '1', '0', '0', '1', '1']
money=5780
day=0
food=333
water=178
weight = 1200
weight_water=3
weight_food=2
money_water=5
money_food=10
cost_stay = [[5, 7], [8, 6], [10, 10]]  # 每天花费
cost_move = [[10, 14], [16, 12], [20, 20]]
in_store=False
dao_store=False
dao_kuang = False
store_water = 0
store_food = 0
store_money = 0
flag = 0
store_cun = False
information = False
infTime = 15

def restart():
    global weight ,day,money,food,water,in_store,dao_store,dao_kuang,store_water,store_food,store_money,flag
    weight = 1200
    day = 0
    money = 5780
    food = 333
    water = 178
    in_store = False
    dao_store = False
    dao_kuang = False
    store_water = 0
    store_food = 0
    store_money = 0
    flag=0

screen=p.display.set_mode((width,height))
store_in = p.image.load('store_in.png').convert_alpha()
store_an = p.image.load('store_an.png').convert_alpha()
bg = p.image.load('background.png').convert_alpha()
store = p.image.load('store.png').convert_alpha()
pic_restart = p.image.load('restart.png').convert_alpha()
ren1 = p.image.load('ren1.png').convert_alpha()
ren2 = p.image.load('ren2.png').convert_alpha()
pic_kuang=p.image.load('kuang.png').convert_alpha()
kuang_an=p.image.load('kuang_an.png').convert_alpha()
over=p.image.load('gameover.png').convert_alpha()

def shuju(weight,day,food,water,money):
    color = (0, 0, 0)#黑色
    wen_weight = font.render('负重:{}'.format(weight), True, color)
    wen_water = font.render('水:{}'.format(water), True, color)
    wen_day = font.render('第{}天'.format(day), True, color)
    wen_food = font.render('食物:{}'.format(food), True, color)
    wen_money = font.render('财产:{}'.format(money), True, color)
    allwen = [wen_weight, wen_water, wen_day, wen_food, wen_money]
    wenpos = [(10, 0), (270, 0), (450, 0), (10, 30), (240, 30)]
    j=0
    for i in allwen:
        screen.blit(i, (wenpos[j]))
        j+=1

def cost_thing():
    global weight, day, food, water, money
    daliycost = cost_move[eval(weather[day - 1])]
    return daliycost


class P():
    def __init__(self):
        self.pos = [116, 52]  # 人物起点的坐标
        self.ren1 = ren1
        self.ren2 = ren2

    def move(self,x,y):
        global weight ,day, money, water, food, weight
        dx = x - self.pos[0] - 20
        # 为啥要减20,30
        dy = y - self.pos[1] - 30
        # 将位置点的移动
        self.pos[0] += dx
        self.pos[1] += dy
        day += 1
        weight = weight - cost_thing()[0] * weight_water - cost_thing()[1] * weight_food
        water -= cost_thing()[0]
        food -= cost_thing()[1]
    def restart(self, x, y):
        self.pos = [116, 52]  # 人物起点的坐标
        self.ren1 = ren1
        self.ren2 = ren2
        dx = x - self.pos[0] - 20
        dy = y - self.pos[1] - 30
        # 位置点移动
        self.pos[0] += dx
        self.pos[1] += dy
    def wakuang(self):
        global day, money, water, food, weight
        day += 1
        weight = weight - 3 / 2 * cost_thing()[0] * weight_water - 3 / 2 * cost_thing()[1] * weight_food
        water -= cost_thing()[0] * 3 / 2
        food -= cost_thing()[1] * 3 / 2
        money += 1000

myself=P()

#判断是否到达矿山，矿山图标是否变亮
def kuang():
    global dao_kuang
    #矿山位置 488，766
    if 344 - 20 <= myself.pos[0] <= 535 + 90 and 770 - 20 <= myself.pos[1] <= 790 + 50:
        screen.blit(pic_kuang, (40, 600))
        dao_kuang = True
    else:
        screen.blit(kuang_an, (40, 600))
        dao_kuang = False

#判断是否到达商店，商店图标是否变亮
def shang():
    global in_store, store_cun
    #起点的商店
    if 0 <= myself.pos[0] <= 160 and 0 <= myself.pos[1] <= 70:
        screen.blit(store, (40, 500))
        in_store = True
        store_cun = False
    #村庄的商店
    elif 397 <= myself.pos[0] <= 468 and 566 <= myself.pos[1] <= 618:
        screen.blit(store, (40, 500))
        in_store = True
        store_cun = True
    else:
        screen.blit(store_an, (40, 500))
        in_store = False
        store_cun = False

#右上角退出
def get_exit(event):
    if event.type == p.QUIT:
        sys.exit()

#进入商店
def store_chuang():
    global water, weight, food, money, dao_store, store_water, store_food, store_money, flag, store_cun
    #是不是在商店内
    if flag==0:
        store_money=money
        flag=1
    screen_store=p.display.set_mode((755, 836))
    screen_store.blit(store_in,(0,0))
    store_list = [store_water, store_food, store_money]
    store_pos_list = [[362, 190], [362, 390], [285, 772]]
    j=0
    for i in store_list:# i指的是store_water,store_food
        store_wen = store_font.render('{}'.format(str(i)), True, (0, 255, 200))# 青色
        screen_store.blit(store_wen, (store_pos_list[j][0], store_pos_list[j][1]))
        j += 1
    for event in p.event.get():
        if event.type==p.MOUSEBUTTONDOWN:
            # 区别是在起点商店还是在村庄
            if store_cun==True:
                # 水资源以1为单位加
                if 362 <= p.mouse.get_pos()[0] <= 482 and 138 <= p.mouse.get_pos()[1] <= 181:
                    store_water += 1
                    store_money = store_money - money_water * 2
                # 水资源以50为单位加
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 125 <= p.mouse.get_pos()[1] <= 176:
                    store_water += 50
                    store_money = store_money - money_water * 50 * 2
                # 水资源以1为单位减
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 246 <= p.mouse.get_pos()[1] <= 284:
                    store_water -= 1
                    store_money = store_money + money_water * 2
                # 水资源以50为单位减
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 235 <= p.mouse.get_pos()[1] <= 280:
                    store_water -= 50
                    store_money = store_money + money_water * 50 * 2
                # 食物以1为单位加
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 335 <= p.mouse.get_pos()[1] <= 374:
                    store_food += 1
                    store_money = store_money - money_food * 2
                # 食物以50为单位加
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 328 <= p.mouse.get_pos()[1] <= 375:
                    store_food += 50
                    store_money = store_money - money_food * 50 * 2
                # 食物以1为单位减
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 442 <= p.mouse.get_pos()[1] <= 482:
                    store_food -= 1
                    store_money = store_money + money_food * 2
                # 食物以50为单位减
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 435 <= p.mouse.get_pos()[1] <= 482:
                    store_food -= 50
                    store_money = store_money + money_food * 50 * 2
                # sure
                elif 13 <= p.mouse.get_pos()[0] <= 292 and 459 <= p.mouse.get_pos()[1] <= 645:
                    money = money - 2 * (store_water * money_water + store_food * money_food)  # 减去花费的钱
                    water += store_water
                    food += store_food
                    weight = weight + store_food * weight_food + store_water * weight_water  # 显示背包的质量
                    dao_store = False
                    flag = 0
                    store_water = 0
                    store_food = 0
                elif 459 <= p.mouse.get_pos()[0] <= 736 and 545 <= p.mouse.get_pos()[1] <= 631:  # cancel
                    store_water = 0
                    store_food = 0
                    flag = 0
            else:
                if 362 <= p.mouse.get_pos()[0] <= 482 and 138 <= p.mouse.get_pos()[1] <= 181:
                    store_water += 1
                    store_money = store_money - money_water
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 125 <= p.mouse.get_pos()[1] <= 176:
                    store_water += 50
                    store_money = store_money - money_water * 50
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 246 <= p.mouse.get_pos()[1] <= 284:
                    store_water -= 1
                    store_money = store_money + money_water
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 235 <= p.mouse.get_pos()[1] <= 280:
                    store_water -= 50
                    store_money = store_money + money_water * 50
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 335 <= p.mouse.get_pos()[1] <= 374:
                    store_food += 1
                    store_money = store_money - money_food
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 328 <= p.mouse.get_pos()[1] <= 375:
                    store_food += 50
                    store_money = store_money - money_food * 50
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 442 <= p.mouse.get_pos()[1] <= 482:
                    store_food -= 1
                    store_money = store_money + money_food
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 435 <= p.mouse.get_pos()[1] <= 482:
                    store_food -= 50
                    store_money = store_money + money_food * 50
                elif 13 <= p.mouse.get_pos()[0] <= 292 and 459 <= p.mouse.get_pos()[1] <= 645:
                    money = money - (store_water * money_water + store_food * money_food)
                    water += store_water
                    food += store_food
                    weight = weight + store_food * weight_food + store_water * weight_water
                    dao_store = False
                    flag = 0
                    store_water = 0
                    store_food = 0
                elif 459 <= p.mouse.get_pos()[0] <= 736 and 545 <= p.mouse.get_pos()[1] <= 631:  # cancel
                    store_water = 0
                    store_food = 0
                    flag = 0
def normal():
    global weight, day, food, water, money, width, height, myself, infTime, information
    screen = p.display.set_mode((width, height))  # 地图屏幕
    screen.blit(bg, (0, 0))
    screen.blit(pic_restart, (0, 800))
    screen.blit(over, (20, 380))
    shang()
    kuang()
    shuju(weight, day, food, water, money)
    screen.blit(myself.ren1, (myself.pos[0], myself.pos[1]))
    if information:
        # render（内容，是否抗锯齿，字体颜色，字体背景颜色）
        screen.blit(font.render('沙暴天气-走不了', False, (255, 0, 0), None), (myself.pos[0] - 100, myself.pos[1] - 30))
        '''
        infTime 的作用，让它做大概15次循环才允许下一次点击
        如果不设inf，马上置false，沙暴天气的提示会一闪而过，非常快
        一次点击要一秒，而在这一秒会做很多次循环
        '''
        infTime -= 0.1
    if infTime <= 0:
        information = False
        infTime = 20

def stop():  # 沙尘暴
    global water, food, day, weight,information,screen
    water -= cost_stay[2][0]
    food -= cost_stay[2][1]
    day += 1
    weight = weight - cost_stay[2][0] * weight_water - cost_stay[2][1] * weight_food

def gameover():  # 兑换成钱
    global money, water, food, money_food, money_water
    if water > 0:
        money += water * money_water / 2
    if food > 0:
        money += food * money_food / 2
    print('本次游戏剩余财产为:', money)
    sys.exit()

# 循环
while True:
    if day == 31:
        sys.exit()
    for event in p.event.get():
        get_exit(event)
        if event.type == p.MOUSEBUTTONDOWN:
            if 40 <= p.mouse.get_pos()[0] <= 140 and 500 <= p.mouse.get_pos()[1] <= 600:  # 激活商店
                dao_store = True
            elif dao_kuang == True and 42 <= p.mouse.get_pos()[0] <= 175 and 601 <= p.mouse.get_pos()[1] <= 713:
                myself.wakuang()
            elif 9 <= p.mouse.get_pos()[0] <= 146 and 800 <= p.mouse.get_pos()[1] <= 856:  # 重新开始
                restart()
                myself.restart(130, 70)
            elif 40 <= p.mouse.get_pos()[0] <= 140 and 400 <= p.mouse.get_pos()[1] <= 500:
                gameover()
            elif int(weather[day]) == 2:
                information = True
                stop()
            elif dao_store == False and information==False:
                myself.move(p.mouse.get_pos()[0], p.mouse.get_pos()[1])
    if dao_store == True and in_store == True:
        store_chuang()
    else:
        normal()
    p.display.update()
